﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Foothill.Common.Logging;
using Foothill.DataAccess.Interface;
using Foothill.Model;

namespace Foothill.DataAccess.Repository
{
    public class PhotoRepo : DbAccessBase, IPhotoRepo
    {
        public void InsertPhotos(IList<UploadPhoto> photoList)
        {
            foreach (UploadPhoto photo in photoList)
            {
                InsertPhoto(photo);
            }
        }
     
        private void InsertPhoto(UploadPhoto photo)
        {
            DbCommand dbCommand = FoothillDB.GetStoredProcCommand("InsertPhoto");

            FoothillDB.AddInParameter(dbCommand, "OriginalName", DbType.String, photo.OriginalName);
            FoothillDB.AddInParameter(dbCommand, "PathOnServer", DbType.String, photo.PathOnServer);
            FoothillDB.AddInParameter(dbCommand, "PhotoSize", DbType.Int32, photo.PhotoSize);
            FoothillDB.AddInParameter(dbCommand, "UploadDateTime", DbType.DateTime, photo.UploadDateTime);
            FoothillDB.AddInParameter(dbCommand, "AdTypeId", DbType.Int32, photo.AdTypeId);
            FoothillDB.AddInParameter(dbCommand, "AdId", DbType.Int32, photo.AdId);
            FoothillDB.AddInParameter(dbCommand, "DisplayPosition", DbType.Int32, photo.DisplayIndex);

            try
            {
                FoothillDB.ExecuteNonQuery(dbCommand);
            }
            catch (Exception exception)
            {
                Log.Error(FoothillModule.DataAccess, exception.Message, exception);
                throw new DataAccessException("Error in insert new photo", exception);
            }
        }

        public IList<UploadPhoto> GetForSaleAdUploadedPhotos(int postId)
        {
            IList <UploadPhoto> result =  new List<UploadPhoto>();

            string sql = @"SELECT [PhotoId], [OriginalName], [PathOnServer], [PhotoSize]
                ,[UploadDateTime], [AdTypeId], [AdId], [DisplayPosition]
	            FROM [Photo] WHERE [AdId] = @PostID";

            DbCommand dbCommand = FoothillDB.GetSqlStringCommand(sql);

            FoothillDB.AddInParameter(dbCommand, "PostID", DbType.Int32, postId);

            try
            {
                using (IDataReader reader = FoothillDB.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        UploadPhoto photo = new UploadPhoto();

                        photo.PhotoId = Convert.ToInt32(reader["PhotoId"]);
                        photo.OriginalName = GetDbString(reader["OriginalName"], string.Empty);
                        photo.PathOnServer = GetDbString(reader["PathOnServer"], string.Empty);
                        photo.PhotoSize = Convert.ToInt32(reader["PhotoSize"]);
                        photo.UploadDateTime = GetDbSmallDateTime(reader["UploadDateTime"]);
                        photo.AdId = postId;
                        photo.DisplayIndex = Convert.ToInt32(reader["DisplayPosition"]);

                        result.Add(photo);
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error(FoothillModule.DataAccess, exception.Message, exception);
                throw new DataAccessException(string.Format("Error in get photos for ad id={0}", postId), exception);
            }
            
            return result;
        }
    }
}
